﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace FinosServiceLibrary.Server {
    class Policy {
        public static byte[] policyBytes;
        public static void PolicyThread(Object stateInfo) {
            Socket policy = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            policy.Bind(new IPEndPoint(IPAddress.Any, 943));
            policy.Listen(10);
            while (true) {
                Socket _policy = policy.Accept();
                string policyRequestString = "<policy-file-request/>";
                byte[] b = new byte[policyRequestString.Length];
                _policy.Receive(b);
                string requeststring = System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
                Logger.trace(requeststring);
                if (requeststring == policyRequestString) {
                    SendPolicy(_policy);
                    Logger.trace("Policy File have sended");
                    _policy.Close();
                } else {
                    Logger.trace("not a sure request string!");
                }
            }
        }
        static void SendPolicy(Socket socket) {
            socket.Send(policyBytes, policyBytes.Length, SocketFlags.None);
        }
    }
}
